package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.MjBillAddress;
import com.moji.entity.MjCar;
import com.moji.entity.dto.QueryBillAddressDto;
import com.moji.entity.dto.QueryCarDto;
import com.moji.mapper.MjBillAddressMapper;
import com.moji.service.MjBillAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.util.IdGenerator;
import com.moji.util.MoJiCommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 发票地址表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-08-21
 */
@Service
public class MjBillAddressServiceImpl extends ServiceImpl<MjBillAddressMapper, MjBillAddress> implements MjBillAddressService {

    @Resource
    private IdGenerator idGenerator;

    @Override
    public MyPage<MjBillAddress> queryByPage(QueryBillAddressDto dto) {
        LambdaQueryWrapper<MjBillAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(dto.getCompanyName()),MjBillAddress::getCompanyName
                ,dto.getCompanyName());
        wrapper.eq(MjBillAddress::getCreateId,MoJiCommonUtils.getUserCode());
        //创建时间倒叙
        wrapper.orderByDesc(MjBillAddress::getCreateTime);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<MjBillAddress> mjBillAddressList = list(wrapper);
        PageInfo<MjBillAddress> info = new PageInfo<>(mjBillAddressList);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public boolean delete(List<Long> ids) {
        if(ObjectUtil.isEmpty(ids)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        ids.forEach(id->{
            this.removeById(id);
        });
        return true;
    }

    @Override
    public boolean add(MjBillAddress mjBillAddress) {
        mjBillAddress.setCreateId(MoJiCommonUtils.getUserCode());
        mjBillAddress.setUpdateId(MoJiCommonUtils.getUserCode());
        mjBillAddress.setCreateTime(new Date());
        mjBillAddress.setUpdateTime(new Date());
        return this.save(mjBillAddress);
    }

    @Override
    public boolean edit(MjBillAddress mjBillAddress) {
        MjBillAddress mjBillAddress1=this.getById(mjBillAddress.getId());
        if(mjBillAddress1 ==null){
            throw new RuntimeException("数据不存在,请核查!");
        }
        BeanUtils.copyProperties(mjBillAddress,mjBillAddress1);
        mjBillAddress1.setUpdateId(MoJiCommonUtils.getUserCode());
        mjBillAddress1.setUpdateTime(new Date());
        return this.updateById(mjBillAddress1);
    }

    @Override
    public MjBillAddress detail(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.getById(id);
    }
}
